package com.wenc.psi.rpstr;

import android.content.Context;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;

import androidx.annotation.NonNull;
import androidx.lifecycle.MutableLiveData;
import com.wenc.psi.dao.EmployeeDao;
import com.wenc.psi.pojo.Employee;

public class PersonRepository {
    private EmployeeDao employeeDao;
    private MutableLiveData<Employee> employeeLive;
    private Employee employee;

    public MutableLiveData<Employee> getEmployeeLive() {
        return employeeLive;
    }

    private final Handler handler = new Handler(Looper.myLooper()){
        @Override
        public void handleMessage(@NonNull Message msg) {
            super.handleMessage(msg);
            if (msg.what == 200) {
                employee = (Employee) msg.obj;
                employeeLive.setValue(employee);
            } else if (msg.what == 400){
                System.out.println("获取不到数据");
            }
        }
    };

    public PersonRepository(Context context) {
        employeeDao = new EmployeeDao();
        employeeLive = new MutableLiveData<>();
    }

    public Employee login(String userid, String password){
        return employeeDao.login(userid, password);
    }

    public void getEmployeeById(String id){
        // 开启线程
        new Thread(()->{
            // 创建消息
            Message message = new Message();
            // 获取员工信息
            Employee employee = employeeDao.getDataById(id);
            if (employee != null){
                message.what = 200;
                message.obj = employee;
            } else {
                message.what = 400;
            }
            // 发送消息
            handler.sendMessage(message);
        }).start();
    }
}
